load("@rules_rust//rust:defs.bzl", "rust_doc_test", "rust_library", "rust_test")
load("//bazel:defs.bzl", "rust_test_suite_with_extra_srcs")
load("//bazel:prost.bzl", "generated_files_check")

package(default_visibility = ["//rs/crypto:__subpackages__"])

filegroup(
    name = "protos",
    srcs = glob(["proto/**"]),
)

DEPENDENCIES = [
    # Keep sorted.
    "//packages/ic-ed25519",
    "//rs/config",
    "//rs/crypto/internal/crypto_lib/basic_sig/ecdsa_secp256k1",
    "//rs/crypto/internal/crypto_lib/basic_sig/ecdsa_secp256r1",
    "//rs/crypto/internal/crypto_lib/basic_sig/ed25519",
    "//rs/crypto/internal/crypto_lib/basic_sig/rsa_pkcs1",
    "//rs/crypto/internal/crypto_lib/bls12_381/vetkd",
    "//rs/crypto/internal/crypto_lib/multi_sig/bls12_381",
    "//rs/crypto/internal/crypto_lib/seed",
    "//rs/crypto/internal/crypto_lib/threshold_sig/bls12_381",
    "//rs/crypto/internal/crypto_lib/threshold_sig/canister_threshold_sig",
    "//rs/crypto/internal/crypto_lib/tls",
    "//rs/crypto/internal/crypto_lib/types",
    "//rs/crypto/internal/logmon",
    "//rs/crypto/internal/test_vectors",
    "//rs/crypto/node_key_validation",
    "//rs/crypto/secrets_containers",
    "//rs/crypto/sha2",
    "//rs/crypto/standalone-sig-verifier",
    "//rs/crypto/tls_interfaces",
    "//rs/interfaces",
    "//rs/monitoring/adapter_metrics/client",
    "//rs/monitoring/logger",
    "//rs/monitoring/metrics",
    "//rs/protobuf",
    "//rs/sys",
    "//rs/types/types",
    "@crate_index//:base64",
    "@crate_index//:bincode",
    "@crate_index//:bytes",
    "@crate_index//:futures",
    "@crate_index//:hex",
    "@crate_index//:parking_lot",
    "@crate_index//:prost",
    "@crate_index//:rand",
    "@crate_index//:rand_chacha",
    "@crate_index//:rayon",
    "@crate_index//:serde",
    "@crate_index//:serde_bytes",
    "@crate_index//:serde_cbor",
    "@crate_index//:slog",
    "@crate_index//:strum",
    "@crate_index//:stubborn-io",
    "@crate_index//:tarpc",
    "@crate_index//:tempfile",
    "@crate_index//:thiserror",
    "@crate_index//:time",
    "@crate_index//:tokio",
    "@crate_index//:tokio-serde",
    "@crate_index//:tokio-util",
    "@crate_index//:tracing",
    "@crate_index//:x509-parser",
    "@crate_index//:zeroize",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//rs/crypto/internal/crypto_lib/threshold_sig/canister_threshold_sig/test_utils",
    "//rs/crypto/internal/crypto_service_provider/csp_proptest_utils",
    "//rs/crypto/internal/csp_test_utils",
    "//rs/crypto/node_key_generation",
    "//rs/crypto/temp_crypto/temp_vault",
    "//rs/crypto/test_utils",
    "//rs/crypto/test_utils/canister_threshold_sigs",
    "//rs/crypto/test_utils/keys",
    "//rs/crypto/test_utils/local_csp_vault",
    "//rs/crypto/test_utils/metrics",
    "//rs/crypto/test_utils/reproducible_rng",
    "//rs/crypto/utils/basic_sig",
    "//rs/test_utilities/in_memory_logger",
    "//rs/test_utilities/time",
    "//rs/types/management_canister_types",
    "//rs/types/types_test_utils",
    "@crate_index//:assert_matches",
    "@crate_index//:mockall",
    "@crate_index//:num_cpus",
    "@crate_index//:proptest",
    "@crate_index//:rsa",
    "@crate_index//:slog-async",
    "@crate_index//:tracing-subscriber",
]

MACRO_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:educe",
    "@crate_index//:strum_macros",
]

DEV_MACRO_DEPENDENCIES = [
    # Keep sorted.
    "@crate_index//:proptest-derive",
]

rust_library(
    name = "crypto_service_provider",
    srcs = glob([
        "src/**/*.rs",
    ]),
    crate_name = "ic_crypto_internal_csp",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES,
)

rust_test(
    name = "crypto_service_provider_test",
    crate = ":crypto_service_provider",
    data = [
        "test_resources/public_keys.pb",
        "test_resources/sks_data_v2.pb",
        "test_resources/sks_data_v3.pb",
    ],
    env = {
        "CARGO_MANIFEST_DIR": "rs/crypto/internal/crypto_service_provider",
    },
    proc_macro_deps = DEV_MACRO_DEPENDENCIES,
    deps = DEV_DEPENDENCIES,
)

rust_test_suite_with_extra_srcs(
    name = "crypto_service_provider_integration",
    srcs = glob(
        ["tests/**/*.rs"],
        exclude = [
            "tests/check_generated_files.rs",
            "tests/common/*.rs",
        ],
    ),
    extra_srcs = glob(["tests/common/*.rs"]),
    proc_macro_deps = MACRO_DEPENDENCIES + DEV_MACRO_DEPENDENCIES,
    deps = [":crypto_service_provider"] + DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_doc_test(
    name = "crypto_service_provider_doc_test",
    crate = ":crypto_service_provider",
)

generated_files_check(
    name = "check_generated_files",
    srcs = ["tests/check_generated_files.rs"],
    data = glob(["src/gen/**"]) + [":protos"],
    manifest_dir = "rs/crypto/internal/crypto_service_provider",
    deps = [
        # Keep sorted.
        "//rs/crypto/internal/crypto_service_provider/protobuf_generator:lib",
        "//rs/test_utilities/compare_dirs",
        "@crate_index//:tempfile",
    ],
)
